<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
      
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="商机名称">
          <a-input placeholder="请输入商机名称" v-decorator="['busName', validatorRules.busName ]" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="客户名称">
          <a-select
              show-search
              placeholder="请选择"
              option-filter-prop="children"
              style="width: 100%"
              :filter-option="filterOption"
              @change="handleChange"
              v-decorator="['cusId', { rules: [{ required: true, message: '请选择客户!' }] }]"
            >
            <a-select-option v-for="t in cusIdOptions" :key="t.value">{{t.text}}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="商机金额">
          <a-input-number v-decorator="[ 'money', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="预计签约">
          <a-date-picker showTime format='YYYY-MM-DD HH:mm:ss' v-decorator="[ 'dealDate', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="负责人ID">
          <a-input placeholder="请输入负责人ID" v-decorator="['ownerUserId', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="需求描述">
          <a-textarea placeholder="请输入需求描述" v-decorator="['usedBrand', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="竞品分享">
          <a-textarea placeholder="请输入竞品分享" v-decorator="['competitiveBrands', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="备注">
          <a-textarea placeholder="请输入备注" v-decorator="['remark', {}]" />
        </a-form-item>
        
		
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"

  export default {
    name: "BusModal",
    data () {
      return {
        cusIdOptions:[],
        busIdOptions:[],
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        busName:{rules: [{ required: true, message: '请输入商机名称!' }]},
        createUserId:{rules: [{ required: true, message: '请输入创建人ID!' }]},
        },
        url: {
          add: "/crm/bus/add",
          edit: "/crm/bus/edit",
          getcusId: "/crm/cus/getcusId",
        },
      }
    },
    created () {
      this.getcusIdOptions();
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'busName','cusId','money','ownerUserId','usedBrand','competitiveBrands','contactsId','status','abandon','remark','createUserId'))
		  //时间格式化
          this.form.setFieldsValue({dealDate:this.model.dealDate?moment(this.model.dealDate):null})
          this.form.setFieldsValue({nextTime:this.model.nextTime?moment(this.model.nextTime):null})
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            formData.dealDate = formData.dealDate?formData.dealDate.format('YYYY-MM-DD HH:mm:ss'):null;
            formData.nextTime = formData.nextTime?formData.nextTime.format('YYYY-MM-DD HH:mm:ss'):null;
            
            console.log(formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },

      getcusIdOptions(){
        httpAction(this.url.getcusId, '', 'get')
          .then((res) => {
            if (res.success) {
             this.cusIdOptions=res.result;
            } else {
              that.$message.warning(res.message)
            }
          })
          
      },
      handleChange(value) {
            console.log(`selected ${value}`);
      },
      filterOption(input, option) {
        return (
         option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },
    }
  }
</script>

<style lang="less" scoped>

</style>